﻿#pragma once
// Copyright (c) lanyeo 2020-2021
// Licenced under MIT license. See https://opensource.org/licenses/MIT for details.
// code style
// 1. use forward declaration (尽可能使用前置声明)
// 2. include file sequence [other project>c>c++>third>self project]
// 

#include <util/fist-time-inl.h>
#include <core/network/network_info.h>

#include <cppcoro/single_consumer_event.hpp>

#include "service_token.h"


#define RPC_IMPL(THIS_INTERFACE) \
private: \
    std::unique_ptr<IRmiStubImpl> __rpc_handler__ = std::make_unique<CRmiStubWrapper<THIS_INTERFACE>>(this); \
    void __handle_remote_call__(const RemoteCallMessage& call) override \
    { \
        __rpc_handler__->__handle_remote_call__(call); \
    } \
    cppcoro::task<> __handle_remote_co_call__(const RemoteCallMessage& call, std::string& back) override \
    { \
        co_await __rpc_handler__->__handle_remote_co_call__(call, back); \
    } \
    IRmiStubImpl* __rmi__() override \
    { \
        return __rpc_handler__.get(); \
    } \

struct service_token_t;

enum RpcMessageType
{
    RPC_MSG_TYPE_REMOTE_CALL = 1,
    RPC_MSG_TYPE_REMOTE_CALL_RETURN = 2,
    RPC_MSG_TYPE_KEEP_ALIVE = 3,
};

struct RpcConf
{
    RpcConf()
        : co_event_timeout(0)
        , keep_server_alive(0)
        , delay_del_co_event(0)
    {
        
    }
    uint32_t co_event_timeout;
    uint32_t keep_server_alive;
    uint32_t delay_del_co_event;
};

struct RpcRemotePeerInfo
{
    int type;
    int id;
    netlink_t link;
    fist::ms_t last_ack;
};

typedef std::unordered_map<service_token_t, RpcRemotePeerInfo, service_token_t::HashFunc, service_token_t::EqualKey>
        RpcRemotePeerMap;



struct RpcRemoteCoCallRecord
{
    RpcRemoteCoCallRecord()
        : returned(false)
        , release(INT64_MAX)
    {
        
    }
    bool returned;
    fist::ms_t release;
    std::string return_data;
    cppcoro::single_consumer_event lock;
};
